package org.osgifx;

import javafx.lang.FX;
import javafx.scene.control.Label;

import org.osgi.framework.*;
import org.osgifx.ui.BundleOverview;



import org.osgifx.view.JavaFxView;

import javafx.scene.Node;
import org.osgifx.ui.ApplicationEntryUI;

/**
 * @author johan
 */

public class FxServiceListener extends ServiceListener, BundleListener {

  public-init var bundleOverview : BundleOverview;
  public-init var bundleContext: BundleContext;

  override function serviceChanged (evt: ServiceEvent) {
    println ("[KFiX] Service changed: {evt}");
    var label: Label = Label {
      text: "service changed, type = {evt.getType()}: {evt}"
    }
    FX.deferAction (
      function () {
        insert label into Main.eventListBox.content;
        if (evt.getType() == ServiceEvent.REGISTERED) {
          var ref: ServiceReference = evt.getServiceReference();
          var myService: JavaFxView =  bundleContext.getService(ref) as JavaFxView;

          var appui: ApplicationEntryUI = ApplicationEntryUI {
            app: myService
          };
          insert appui into Main.applicationOverviewScreen.appUI;
         // insert myNode into Main.serviceWindow.content;
        }

      }
    );
  }

  override function bundleChanged (evt: BundleEvent) {
    println ("[KFiX] Bundle changed: {evt}");
    var bundle : Bundle  = evt.getBundle();
    FX.deferAction (
      function () {
        if (evt.getType()== BundleEvent.INSTALLED) {
          bundleOverview.addBundle (bundle);
        }
        var label: Label = Label {
          text: "bundle changed: {evt}"
        }
        insert label into Main.eventListBox.content;
      }
    );
  }

}
